package com.nl.controller.xuyi.service;

import com.alibaba.fastjson.JSONObject;
import com.nl.controller.common.model.BaseResult;
import com.nl.controller.web.bean.BdResult;
import com.nl.controller.web.util.DoubleUtil;
import com.nl.controller.web.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhangheng
 * @Desc:
 * @Date: 16:35 2020/9/8
 * @Modified By:
 */
@Slf4j
public class CommonService {

    public static String httpPostWithJson(String url, JSONObject jsonParam)
            throws Exception {
        DefaultHttpClient httpclient = null;
        HttpPost httpPost = null;
        HttpResponse response = null;
        org.apache.http.HttpEntity entity = null;
        String result = "";
        try {
            httpclient = new DefaultHttpClient();
            httpPost = new HttpPost(url);
            // 解决中文乱码问题
            StringEntity entityParam = new StringEntity(jsonParam.toString(),"utf-8");
            entityParam.setContentEncoding("UTF-8");
            entityParam.setContentType("application/json");
            httpPost.setEntity(entityParam);
            // 设置连接超时时间
            HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),
                    100000);
            // 设置读数据超时时间
            HttpConnectionParams.setSoTimeout(httpclient.getParams(), 100000);
            response = httpclient.execute(httpPost);
            entity = response.getEntity();
            if (null != entity) {
                byte[] bytes = EntityUtils.toByteArray(entity);
                result = new String(bytes, "UTF-8");
            }
            return result;
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != httpclient) {
                httpclient.getConnectionManager().shutdown();
            }
        }
    }


    public static String BdToGPS(String lngAndLat) throws UnsupportedEncodingException {
        String[] split = lngAndLat.split(",");
        String lng = split[0];
        String lat = split[1];
        String url = "http://api.map.baidu.com/ag/coord/convert";
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("from", "0");
        querys.put("to", "4");
        querys.put("x", lng);
        querys.put("y", lat);
        BaseResult<String> sendGet = HttpClientUtil.sendGet(url, querys, "utf-8", null);
        if ("0".equals(sendGet.getRetCode())) {
            BdResult data =  JSONObject.parseObject(sendGet.getData(), BdResult.class) ;
            if ("0".equals(data.getError())) {
                String x = new String(Base64Utils.decode(data.getX().getBytes()), "utf-8");
                String y = new String(Base64Utils.decode(data.getY().getBytes()), "utf-8");
                Double lngGPS = DoubleUtil.sub(DoubleUtil.mul(2.0, Double.parseDouble(lng)),Double.parseDouble(x));
                Double latGPS =DoubleUtil.sub(DoubleUtil.mul(2.0, Double.parseDouble(lat)),Double.parseDouble(y));
                return lngGPS.toString()+","+latGPS.toString();
            }
        }
        return lng+","+lat;
    }
}
